<template>
  <div class="fillcontain">
    <div class="filter-container">
      <!--<el-select clearable v-model="listQuery.phone_type" placeholder="设备类型">
        <el-option v-for="item in deviceTypeOptions" :key="item.id" :label="item.name" :value="item.id"></el-option>
      </el-select>

      <el-button type="primary" icon="search" @click="handleFilter">查询</el-button>
      <el-button type="warning" @click="resetFilter">重置</el-button>-->
      <el-button class="filter-item" @click="handleBanner('', 'add')" type="primary" icon="edit">
        添加
      </el-button>
    </div>

    <div class="app-container calendar-list-container">
      <el-table :data="list" border fit highlight-current-row v-loading="listLoading"
                element-loading-text="拼命加载中" element-loading-spinner="el-icon-loading">
        <el-table-column align="center" label="内容源名称">
          <template slot-scope="scope">
            <span>{{scope.row.spiderType | spiderTypeFilter}}</span>
          </template>
        </el-table-column>

        <el-table-column align="center" label="内容源分类" width="120">
          <template slot-scope="scope">
            <span>{{scope.row.subType}}</span>
          </template>
        </el-table-column>

        <el-table-column align="center" label="链接地址" width="400" class-name="updateInfo">
          <template slot-scope="scope">
            <span>{{scope.row.link}}</span>
          </template>
        </el-table-column>



        <el-table-column align="center" label="接口地址" width="400" class-name="updateInfo">
          <template slot-scope="scope">
            <span>{{scope.row.apiUrl}}</span>
          </template>
        </el-table-column>

        <el-table-column align="center" label="操作" width="120">
          <template slot-scope="scope">
            <el-button size="small" type="primary"
                       @click="handleBanner(scope.row, 'modify')">编辑
            </el-button>
            <el-button size="small" type="danger"
                       @click="handleBanner(scope.row, 'delete')">删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>

     <TablePagination
      @handleSizeChange="handleSizeChange"
      @handleCurrentChange="handleCurrentChange"
      :current-page="listQuery.page"
      :limit="listQuery.limit"
      :total="total"
      :loading="listLoading"
    >
    </TablePagination>

    <el-dialog :title="operationFormTitle" :visible.sync="operationFormVisible">
      <el-form :rules="rules" class="small-space" :model="operationInfo" label-position="left" label-width="100px" ref="operationInfo">
        <el-form-item label="内容源" prop="spiderType">
          <el-select clearable v-model="operationInfo.spiderType" placeholder="请选择内容源">
            <el-option v-for="item in spiderTypeOptions" :key="item.id" :label="item.text" :value="item.id"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="内容源分类" prop="subType">
          <el-input v-model="operationInfo.subType"></el-input>
        </el-form-item>

        <el-form-item label="链接地址" prop="link">
          <el-input v-model="operationInfo.link"></el-input>
        </el-form-item>

        <el-form-item label="接口地址" prop="apiUrl" min-width="200">
          <el-input v-model="operationInfo.apiUrl"></el-input>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="operationFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="confirm">确 定</el-button>
      </div>
    </el-dialog>


  </div>
</template>
<script>
import TablePagination from "@/components/TablePagination";
  export default {
    name: 'spiderSource',
    components: { TablePagination },
    data() {
      return {
        list: null,
        total: null,
        listLoading: true,
        listQuery: {
          offset: 0,
          limit: 20
        },
        operationFormTitle: '',
        operationFormVisible: false,
        operationInfo: {},
        spiderTypeOptions:[{id:3,text:'微博精选'}],
        rules: {
          spiderType: [{required: true, message: '不能为空', trigger: 'change'}],
          subType: [{required: true, message: '不能为空', trigger: 'change'}],
          link: [{required: true, message: '不能为空', trigger: 'change'}],
          apiUrl: [{required: true, message: '不能为空', trigger: 'change'}],
        },
      }
    },
    filters: {
      spiderTypeFilter(id) {
        const map = {
          '3': '微博精选',
        }
        return map[id]
      },
    },
    created() {
      this.getList()
    },
    methods: {
      getList() {
        this.listLoading = true
        this.$get('spiderSource/list', this.listQuery).then(resp => {
          this.list = resp.data.items
          this.total = resp.data.count
          this.listLoading = false
        })
      },

      handleBanner(row, type) {
        if (type === 'add' || type === 'modify') {
          this.operationFormVisible = true
          if (type === 'add') {
            this.$nextTick(() => {
              this.$refs['operationInfo'].resetFields()
              this.operationFormTitle = '新增'
              this.operationInfo = {
                type: 'add',
                spiderType: '',
                subType: '',
                link: '',
                apiUrl: '',
              }
            })
          } else {
            this.operationFormTitle = '编辑'
            this.operationInfo = {
              type: 'modify',
              id:row.id,
              spiderType: row.spiderType,
              subType: row.subType,
              link: row.link,
              apiUrl: row.apiUrl
            }
          }
        } else {
          this.$confirm(`确认删除此爬虫来源？`, '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }).then(() => {
            this.$get('spiderSource/delete', {id: row.id}).then(resp => {
              if (resp.code === 0) {
                this.getList()
                this.$message({
                  message: '操作成功',
                  type: 'success'
                })
                this.operationFormVisible = false
              } else {
                this.$message({
                  message: resp.message,
                  type: 'error'
                })
              }
            })
          }).catch(()=>{})
        }
      },
      confirm() {
        this.$refs['operationInfo'].validate((valid) => {
          if (valid) {
            const params = {
              spiderType: this.operationInfo.spiderType,
              subType: this.operationInfo.subType,
              link: this.operationInfo.link,
              apiUrl: this.operationInfo.apiUrl,
            }
            if (this.operationInfo.type == 'modify') {
              params.id = this.operationInfo.id
              this.$post('spiderSource/update', params).then(resp => {
                if (resp.code === 0) {
                  this.getList()
                  this.$message({
                    message: '操作成功',
                    type: 'success'
                  })
                  this.operationFormVisible = false
                } else {
                  this.$message({
                    message: resp.message,
                    type: 'error'
                  })
                }
              })
            }

            if (this.operationInfo.type == 'add') {
              this.$post('spiderSource/add', params).then(resp => {
                if (resp.code === 0) {
                  this.getList()
                  this.$message({
                    message: '操作成功',
                    type: 'success'
                  })
                  this.operationFormVisible = false
                } else {
                  this.$message({
                    message: resp.message,
                    type: 'error'
                  })
                }
              })
            }
          } else {
            console.log('error submit!!');
            return false;
          }
        });

      }
    }
  }
</script>
<style>
  .el-table td.updateInfo .cell{
    text-overflow: inherit !important;
    white-space: normal !important;
    padding:0 10px;
  }
</style>
